package com.yuanqi.Common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.yuanqi.Common.Interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Resource
    private UserInterceptor userIntorceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIntorceptor)
                .excludePathPatterns(List.of("/v1/post/getList", "/v1/post/getDetail", "/v1/topic/**", "/v1/adsense/**", "/v1/comment", "/chat/**"))
                .excludePathPatterns(List.of("/v1/post/getPostByClassId", "/v1/post/getPostByUserId", "/v1/post/SearchPost", "/update/check"))
                .addPathPatterns("/v1/comment/AddComment", "/v1/user/updateUserInfo")
                .addPathPatterns("/v1/feedback/**", "/v1/upload")
                .addPathPatterns("/v1/follow/**", "/v1/post/delImg", "/v1/post/publish", "/v1/post/getPostByLoginUser")
                .addPathPatterns("/v1/support")
                .addPathPatterns("/v1/black/**")
                .addPathPatterns("/v1/chat/**")
                .addPathPatterns("/v1/post/getConcernPost")
                .addPathPatterns("/v1/user/updateUserInfo", "/v1/user/updatePass");
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //构建分页插件(指定类型)
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        //这里是设置最大pagesize,默认是500,这里设置为-1,表示不限制
        paginationInnerInterceptor.setMaxLimit(-1L);

        //添加插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }
}
